package com.zzptc.zzsgz.bdsjws.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.InsertLianXiRenActivity;
import com.zzptc.zzsgz.bdsjws.adapter.SOSAdater;
import com.zzptc.zzsgz.bdsjws.fragments.base.BaseFragmentbase;
import com.zzptc.zzsgz.bdsjws.vo.Contact;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by qqh on 2017/5/26.
 */

public class Fragment_family2 extends BaseFragmentbase {


    @BindView(R.id.image_sos)
    ImageView imageSos;
    @BindView(R.id.ET_sosadd)
    EditText ETSosadd;
    @BindView(R.id.btn_sosadd)
    Button btnSosadd;
    @BindView(R.id.rv_SosTel)
    LinearLayout rvSosTel;
    Unbinder unbinder;


    @Override
    public int getXML() {
        return R.layout.activity_fragment2;
    }

    @Override
    public void initView() {
        ETSosadd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {//改变之前

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {//改变
                imageSos.setVisibility(View.GONE);
                btnSosadd.setVisibility(View.VISIBLE);
            }

            @Override
            public void afterTextChanged(Editable editable) {//改变之后

            }
        });
    }

    @Override
    public void initDate() {

    }


    @OnClick(R.id.image_sos)
    public void onViewClicked() {
        startActivity(new Intent(getActivity(), InsertLianXiRenActivity.class));
    }


    @OnClick(R.id.btn_sosadd)
    public void onYesClicked() {
        //判断电话号码的长度
        String tel = ETSosadd.getText().toString().trim();
        if (tel.length() == 11 && tel.matches("^1[3578]\\d{9}")) {
            //放入集合中
            List<Contact> listSos = MyApplication.getSosList();
            Contact contact = new Contact();
            contact.setPhonenum(tel);
            if (listSos.size()<3){
                listSos.add(contact);
            }else {
                Toast.makeText(getActivity(), "已经有三人", Toast.LENGTH_SHORT).show();
            }
            listSos.add(contact);
            SOSAdater sosAdater = new SOSAdater(listSos);



        }
        ETSosadd.setText("");
        btnSosadd.setVisibility(View.GONE);
        imageSos.setVisibility(View.VISIBLE);

    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
